package com.shared.controller;

import com.shared.pojo.PUser;
import com.shared.server.PUserServer;
import com.shared.util.RestDTO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class PUserController {

    @Autowired
    private PUserServer pUserServer;

    @GetMapping("/user/{phone}")
    @ResponseBody
    @ApiOperation(value = "根据用户手机号查询用户")
    public List<PUser> findUser(@PathVariable("phone") String phone) {

        return pUserServer.findUserByPhone(phone);
    }

    @GetMapping("/users/{id}")
    @ResponseBody
    @ApiOperation(value = "根据用户id查询用户")
    public List<PUser> users(@PathVariable("id") Object id) {
        return pUserServer.findUser(id);
    }

    @PostMapping("/user")
    @ResponseBody
    @ApiOperation(value = "修改用户信息")
    public RestDTO updateUser(@RequestBody PUser pUser) {
        if (pUser.getSex().equals("男") || pUser.getSex().equals("女")) {
            try {
                this.pUserServer.update(pUser);
                return RestDTO.UPDATE_SUCCESS;
            } catch (Exception e) {
                return RestDTO.UPDATE_FAILURE;
            }
        }
        return RestDTO.SEX_FAILURE;
    }
}